package cn.v2tila.rsosa.management.service.serviceImpl;

import cn.v2tila.rsosa.management.entity.MessageEntity;
import cn.v2tila.rsosa.management.jpa.MsgJPA;
import cn.v2tila.rsosa.management.service.MsgService;
import cn.v2tila.rsosa.management.utils.PageDataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author v2tila on 19:17 2019/4/19
 */
@Service
public class MsgServiceImpl implements MsgService {

    @Autowired
    private MsgJPA msgJPA;

    @Override
    public PageDataResult getMsg(int page, int limit, MessageEntity messageEntity) {
        PageDataResult pageDataResult = new PageDataResult();
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(page-1, limit, sort);

        ExampleMatcher matcher;

        if(messageEntity.getId()==0){
            matcher = ExampleMatcher.matching()
//                    .withMatcher("uniqueId", ExampleMatcher.GenericPropertyMatchers.contains())
                    .withIgnorePaths("id");
        }else{
            matcher = ExampleMatcher.matching();
        }
        Example<MessageEntity> example = Example.of(messageEntity,matcher);
        Page<MessageEntity> pages = msgJPA.findAll(example,pageable);
        List<MessageEntity> msgEntityList = pages.getContent();
        int count = msgJPA.findAll(example).size();
        pageDataResult.setData(msgEntityList);
        pageDataResult.setCount(count);
        return pageDataResult;
    }
}
